﻿using SQLDeploy.Data.Internal;
using System;
using System.ComponentModel;
using System.Diagnostics;

using System.Xml;
using System.Xml.Serialization;

namespace SQLDeploy.Data.Connections
{
    [Serializable()]
    public abstract class _BaseConnection : ValidationNotifier, IComparable
    {

        private string _name;
        private string _data;

        protected internal _BaseConnection() 
        {   }

        protected internal _BaseConnection(string name, string data){
            this.Name = name;
            this.Data = data;
        }

        [XmlAttribute]
        public string Name
        {
            get
            {
                return _name;
            }
            protected internal set
            {
                if (string.IsNullOrEmpty(value))
                    throw new ArgumentNullException("Name property cannot be null, or empty");
                if (_name == null || !Internal.Shared.DefaultStringComparer().Equals(_name, value))
                {
                    _name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        [XmlElement]
        public virtual string Data 
        {
            get { return _data; }
            protected internal set 
            {
                if (string.IsNullOrEmpty(value))
                    throw new ArgumentNullException("Data property cannot be null, or empty");
                if (_data == null || !_data.Equals(value, StringComparison.CurrentCulture))
                {
                    _data = value;
                    OnPropertyChanged("Data");
                }
            }
        }

        [XmlIgnore]
        public abstract string Usage
        {
            get;
        }

        [XmlIgnore]
        public string Type { get { return GetType().Name; } }

        
        protected abstract bool PerformConnectionValidation();

        //public override bool Equals(object obj)
        //{
        //    if (obj == null)
        //        return false;
        //    if (!(obj is _BaseConnection))
        //        return false;
        //    _BaseConnection other = (_BaseConnection)obj;
        //    if (other.Type != Type)
        //        return false;
        //    StringComparer sc = Internal.Shared.DefaultStringComparer();
        //    if (sc.Equals(Name, other.Name)) 
        //    {
        //        return DataEquals(other.Data);
        //    }
        //    return false;
        //}

        //public override int GetHashCode()
        //{
        //    return Name.GetHashCode() + Data.GetHashCode();
        //}

        protected internal bool DataEquals(string otherData)
        {
            return Data == otherData;
        }

        public int CompareTo(object obj)
        {
            if (!(obj is _BaseConnection))
                throw new ArgumentException("obj must be _BaseConnection");
            _BaseConnection other = (_BaseConnection)obj;
            StringComparer sc = Internal.Shared.DefaultStringComparer();
            int type = Type.CompareTo(other.Type);
            if (type != 0)
                return type;
            return sc.Compare(Name, other.Name);
        }

        public override bool performValidation()
        {
            return PerformConnectionValidation();
        }
    }
}
